package com.example.service.Impl;

import com.example.Constants.SystemConstants;
import com.example.domain.ResponseResult;
import com.example.domain.dto.IdNameDto;
import com.example.domain.entity.Role;
import com.example.domain.vo.IdNameVo;
import com.example.enums.AppHttpCodeEnum;
import com.example.handler.exception.SystemException;
import com.example.mapper.RoleMapper;
import com.example.mapper.UserMapper;
import com.example.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public ResponseResult getRoleList() {
        List<IdNameVo> idNameVoList = roleMapper.selectAll();
        HashMap<String, Object> idNameVoHashMap = new HashMap<>();
        idNameVoHashMap.put("roleList",idNameVoList);
        return ResponseResult.okResult(idNameVoHashMap);
    }

    @Override
    @Transactional
    public ResponseResult deleteRole(Integer id) {
        //如果删除的id为默认role的id
        if (SystemConstants.DEFAULT_ROLE_ID.equals(id)){
            throw new SystemException(AppHttpCodeEnum.DELETE_ADMIN_ERROR);
        }
        int deleteCount = roleMapper.updateIsDeleteById(id);
        if (deleteCount==0){
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        //修改被删除用户的role为默认值
        userMapper.updateRoleByDeleteRoleId(id);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult addRole(String roleName) {
        //查询当前数据库是否有此职能
        boolean exit = exitRoleName(roleName);
        if (exit){
            throw new SystemException(AppHttpCodeEnum.NAME_ERROR);
        }
        Integer insertNum = roleMapper.addRole(roleName);
        if (insertNum == 0){
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return ResponseResult.okResult();
    }

    public boolean exitRoleName(String roleName){
        Role role = roleMapper.selectByRoleName(roleName);
        if (role != null){
            return true;
        }
        return false;
    }

    @Override
    public ResponseResult modInit(Integer id) {
        Role role = roleMapper.selectByPrimaryKey(Long.valueOf(id));
        if (Objects.isNull(role)){
            throw new SystemException(AppHttpCodeEnum.SELECT_ERROR);
        }
        HashMap<String, String> roleName = new HashMap<>();
        roleName.put("name",role.getRolename());
        return ResponseResult.okResult(roleName);
    }

    @Override
    public ResponseResult modRole(IdNameDto idNameDto) {
        //不可以修改默认的role
        if (SystemConstants.DEFAULT_ROLE_ID.equals(idNameDto.getId())){
            throw new SystemException(AppHttpCodeEnum.DELETE_ADMIN_ERROR);
        }
        //修改权限名称
        int updateNum = roleMapper.updateRoleNameById(idNameDto);
        if (updateNum == 0){
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return ResponseResult.okResult();
    }
}
